package de.suur.jarmi.xstream;

import java.io.Serializable;

import com.thoughtworks.xstream.XStream;

import de.suur.jarmi.common.Request;
import de.suur.jarmi.common.Response;
import de.suur.jarmi.common.Serializer;

public class XStreamSerializer implements Serializer {

	private XStream xstream;

	public XStreamSerializer() {
		xstream = new XStream();
	}

	private <T> T deserialize(byte[] data) {
		String xmlString = new String(data);
		return (T) xstream.fromXML(xmlString);
	}

	@Override
	public Request deserializeRequest(byte[] data) {
		return deserialize(data);
	}

	@Override
	public Response deserializeResponse(byte[] data) {
		return deserialize(data);
	}

	@Override
	public <T extends Serializable> byte[] serialize(T object) {
		String xmlString = xstream.toXML(object);
		return xmlString.getBytes();
	}

}
